package com.example.dialoglibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dialoglibrary.R;

/**
 * 参考：
 * https://blog.csdn.net/suwenlai/article/details/74911005
 * https://www.jianshu.com/p/1ddb69f1f67e
 */
public class CustomDialog extends Dialog {

    private Context mContext;
    private int mHeight, mWidth;
    private boolean mCanceledOnTouchOutside;
    private boolean mCancelable;
    private int mGravity;
    private View mView;
    private int mAnimation;
    private float mDimAmount;

    private CustomDialog(Builder builder) {
        this(builder, R.style.DialogTheme);
    }

    private CustomDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        mContext = builder.context;
        mHeight = builder.height;
        mWidth = builder.width;
        mDimAmount = builder.dimAmount;
        mCancelable = builder.cancelable;
        mCanceledOnTouchOutside = builder.canceledOnTouchOutside;
        mGravity = builder.gravity;
        mView = builder.contentView;
        mAnimation = builder.animation;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(mView);
        setCancelable(mCancelable);
        setCanceledOnTouchOutside(mCanceledOnTouchOutside);

        Window window = getWindow();
        if (window != null) {
            //设置窗口位置
            window.setGravity(mGravity);
            window.setDimAmount(mDimAmount);

            //去掉背景
            window.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), (Bitmap) null));

            //设置窗口进出动画
            if (mAnimation > 0) {
                window.setWindowAnimations(mAnimation);
            }

            WindowManager.LayoutParams layoutParams = window.getAttributes();
            if (layoutParams != null) {
                // 获取屏幕宽、高
                DisplayMetrics d = mContext.getResources().getDisplayMetrics();
                if (mWidth != 0 && mHeight != 0) {
                    layoutParams.width = mWidth;
                    layoutParams.height = mHeight;
                } else {
                    layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                }
            }
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

        Window window = getWindow();
        if (window != null) {
            window.setDimAmount(1.0f);
        }
    }

    /**
     * 获取view
     * @param viewRes
     * @param cls
     * @param <T>
     * @return
     */
    public <T> T getView(int viewRes, Class<T> cls) {
        if (mView != null) {
            return (T) this.mView.findViewById(viewRes);
        }
        return null;
    }

    public static final class Builder {

        private View contentView;
        private int animation;
        private Context context;
        private int height, width;
        private int resStyle = -1;
        private float dimAmount = 0.5f;
        private boolean cancelable = true;
        private int gravity = Gravity.CENTER;
        private boolean canceledOnTouchOutside = true;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContentView(int resView) {
            contentView = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder gravity(int val) {
            gravity = val;
            return this;
        }

        public Builder height(int val) {
            height = val;
            return this;
        }

        public Builder width(int val) {
            width = val;
            return this;
        }

        public Builder dimAmount(float val) {
            dimAmount = val;
            return this;
        }

        public Builder animation(int val) {
            animation = val;
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder canceledOnTouchOutside(boolean val) {
            canceledOnTouchOutside = val;
            return this;
        }

        public Builder cancelable(boolean val) {
            cancelable = val;
            return this;
        }

        /**
         * 添加点击事件
         *
         * @param viewRes  viewId
         * @param listener 监听
         * @return builder
         */
        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            if (contentView != null && listener != null) {
                contentView.findViewById(viewRes).setOnClickListener(listener);
            }
            return this;
        }

        /**
         * 设置文本
         *
         * @param viewRes viewId
         * @param message 文本
         * @return builder
         */
        public Builder setText(int viewRes, String message) {
            if (contentView != null && !TextUtils.isEmpty(message)) {
                View view = this.contentView.findViewById(viewRes);
                if (view instanceof TextView) {
                    ((TextView) view).setText(message);
                }
            }
            return this;
        }

        /**
         * 设置RV
         *
         * @param viewRes viewId
         * @param adapter 适配器
         * @return builder
         */
        public Builder setAdapter(int viewRes, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
            if (contentView != null) {
                View view = this.contentView.findViewById(viewRes);
                if (view instanceof RecyclerView) {
                    ((RecyclerView) view).setAdapter(adapter);
                    ((RecyclerView) view).setLayoutManager(new LinearLayoutManager(context));
                }
            }
            return this;
        }

        public CustomDialog build() {
            if (resStyle != -1) {
                return new CustomDialog(this, resStyle);
            } else {
                return new CustomDialog(this);
            }
        }
    }
}